package org.gdstash.ui.character;

import java.awt.event.ActionEvent;
import java.util.Iterator;
import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharTeleportList;
import java.util.LinkedList;
import org.gdstash.character.GDCharUID;
import java.util.List;
import javax.swing.border.TitledBorder;
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.border.Border;
import org.gdstash.util.GDMsgFormatter;
import javax.swing.BorderFactory;
import org.gdstash.ui.GDStashFrame;
import javax.swing.UIManager;
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.Container;
import javax.swing.GroupLayout;
import javax.swing.JCheckBox;
import org.gdstash.ui.util.GDAbstractRiftPane;

public class GDCharRiftAct7Pane extends GDAbstractRiftPane
{
    private JCheckBox cbFrostveilHighlands;
    private JCheckBox cbIceboundPassage;
    private JCheckBox cbFreyollValley;
    private JCheckBox cbKurnhold;
    private JCheckBox cbUlosSprings;
    private JCheckBox cbDreadWastes;
    private JCheckBox cbBloodFields;
    private JCheckBox cbGlaciersEdge;
    private JCheckBox cbAuroraPeaks;
    private JCheckBox cbGlacialGrotto;
    private JCheckBox cbRoofOfTheWorld;
    private JCheckBox cbDrangSulLowlands;
    private JCheckBox cbDrangGalFortress;
    private int difficulty;
    private boolean changed;
    
    public GDCharRiftAct7Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbFrostveilHighlands).addComponent(this.cbIceboundPassage).addComponent(this.cbFreyollValley).addComponent(this.cbKurnhold).addComponent(this.cbUlosSprings).addComponent(this.cbDreadWastes).addComponent(this.cbBloodFields).addComponent(this.cbGlaciersEdge).addComponent(this.cbAuroraPeaks).addComponent(this.cbGlacialGrotto).addComponent(this.cbRoofOfTheWorld).addComponent(this.cbDrangSulLowlands).addComponent(this.cbDrangGalFortress));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbFrostveilHighlands)).addGroup(layout.createParallelGroup().addComponent(this.cbIceboundPassage)).addGroup(layout.createParallelGroup().addComponent(this.cbFreyollValley)).addGroup(layout.createParallelGroup().addComponent(this.cbKurnhold)).addGroup(layout.createParallelGroup().addComponent(this.cbUlosSprings)).addGroup(layout.createParallelGroup().addComponent(this.cbDreadWastes)).addGroup(layout.createParallelGroup().addComponent(this.cbBloodFields)).addGroup(layout.createParallelGroup().addComponent(this.cbGlaciersEdge)).addGroup(layout.createParallelGroup().addComponent(this.cbAuroraPeaks)).addGroup(layout.createParallelGroup().addComponent(this.cbGlacialGrotto)).addGroup(layout.createParallelGroup().addComponent(this.cbRoofOfTheWorld)).addGroup(layout.createParallelGroup().addComponent(this.cbDrangSulLowlands)).addGroup(layout.createParallelGroup().addComponent(this.cbDrangGalFortress));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        }
        else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbIceboundPassage);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbFreyollValley);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbKurnhold);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbUlosSprings);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbDreadWastes);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbBloodFields);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbGlaciersEdge);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbAuroraPeaks);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbGlacialGrotto);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbRoofOfTheWorld);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbDrangSulLowlands);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbDrangGalFortress);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbIceboundPassage);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbFreyollValley);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbKurnhold);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbUlosSprings);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbDreadWastes);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbBloodFields);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbGlaciersEdge);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbAuroraPeaks);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbGlacialGrotto);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbRoofOfTheWorld);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbDrangSulLowlands);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbDrangGalFortress);
    }
    
    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float)GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float)GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float)GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ACT7"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbFrostveilHighlands == null) {
            (this.cbFrostveilHighlands = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbFrostveilHighlands.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_07_FROSTRVEIL_HIGHLANDS"));
        this.cbFrostveilHighlands.setFont(fntCheck);
        if (this.cbIceboundPassage == null) {
            (this.cbIceboundPassage = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbIceboundPassage.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_07_ICEBOUND_PASSAGE"));
        this.cbIceboundPassage.setFont(fntCheck);
        if (this.cbFreyollValley == null) {
            (this.cbFreyollValley = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbFreyollValley.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_07_FREY0LL_VALLEY"));
        this.cbFreyollValley.setFont(fntCheck);
        if (this.cbKurnhold == null) {
            (this.cbKurnhold = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbKurnhold.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_07_KURNHOLD"));
        this.cbKurnhold.setFont(fntCheck);
        if (this.cbUlosSprings == null) {
            (this.cbUlosSprings = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbUlosSprings.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_07_ULOS_SPRINGS"));
        this.cbUlosSprings.setFont(fntCheck);
        if (this.cbDreadWastes == null) {
            (this.cbDreadWastes = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbDreadWastes.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_07_DREAD_WASTES"));
        this.cbDreadWastes.setFont(fntCheck);
        if (this.cbBloodFields == null) {
            (this.cbBloodFields = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbBloodFields.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_07_BLOOD_FIELDS"));
        this.cbBloodFields.setFont(fntCheck);
        if (this.cbGlaciersEdge == null) {
            (this.cbGlaciersEdge = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbGlaciersEdge.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_07_GLACIERS_EDGE"));
        this.cbGlaciersEdge.setFont(fntCheck);
        if (this.cbAuroraPeaks == null) {
            (this.cbAuroraPeaks = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbAuroraPeaks.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_07_AURORA_PEAKS"));
        this.cbAuroraPeaks.setFont(fntCheck);
        if (this.cbGlacialGrotto == null) {
            (this.cbGlacialGrotto = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbGlacialGrotto.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_07_GLACIAL_GROTTO"));
        this.cbGlacialGrotto.setFont(fntCheck);
        if (this.cbRoofOfTheWorld == null) {
            (this.cbRoofOfTheWorld = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbRoofOfTheWorld.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_07_ROOF_WORLD"));
        this.cbRoofOfTheWorld.setFont(fntCheck);
        if (this.cbDrangSulLowlands == null) {
            (this.cbDrangSulLowlands = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbDrangSulLowlands.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_07_DRANG_SUL_LOW"));
        this.cbDrangSulLowlands.setFont(fntCheck);
        if (this.cbDrangGalFortress == null) {
            (this.cbDrangGalFortress = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbDrangGalFortress.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_07_DRANG_GAL_HIGH"));
        this.cbDrangGalFortress.setFont(fntCheck);
    }
    
    @Override
    public boolean hasChanged() {
        return this.changed;
    }
    
    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }
    
    public void setSelected(final boolean selected) {
        this.cbFrostveilHighlands.setSelected(selected);
        this.cbIceboundPassage.setSelected(selected);
        this.cbFreyollValley.setSelected(selected);
        this.cbKurnhold.setSelected(selected);
        this.cbUlosSprings.setSelected(selected);
        this.cbDreadWastes.setSelected(selected);
        this.cbBloodFields.setSelected(selected);
        this.cbGlaciersEdge.setSelected(selected);
        this.cbAuroraPeaks.setSelected(selected);
        this.cbGlacialGrotto.setSelected(selected);
        this.cbRoofOfTheWorld.setSelected(selected);
        this.cbDrangSulLowlands.setSelected(selected);
        this.cbDrangGalFortress.setSelected(selected);
    }
    
    @Override
    public List<GDCharUID> getRiftList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbFrostveilHighlands.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_07_FROSTRVEIL_HIGHLANDS);
        }
        if (this.cbIceboundPassage.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_07_ICEBOUND_PASSAGE);
        }
        if (this.cbFreyollValley.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_07_FREYOLL_VALLEY);
        }
        if (this.cbKurnhold.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_07_KURNHOLD);
        }
        if (this.cbUlosSprings.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_07_ULOS_SPRINGS);
        }
        if (this.cbDreadWastes.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_07_DREAD_WASTES);
        }
        if (this.cbBloodFields.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_07_BLOOD_FIELDS);
        }
        if (this.cbGlaciersEdge.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_07_GLACIERS_EDGE);
        }
        if (this.cbAuroraPeaks.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_07_AURORA_PEAKS);
        }
        if (this.cbGlacialGrotto.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_07_GLACIAL_GROTTO);
        }
        if (this.cbRoofOfTheWorld.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_07_ROOF_WORLDN);
        }
        if (this.cbDrangSulLowlands.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_07_DRANG_SUL_LOW);
        }
        if (this.cbDrangGalFortress.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_07_DRANG_GAL_HIGH);
        }
        return list;
    }
    
    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        boolean expFoA = GDStashFrame.expansionFangsOfAsterkarn;
        if (gdc != null) {
            expFoA = (expFoA || gdc.isFangsOfAsterkarnChar());
        }
        if (expFoA) {
            this.cbFrostveilHighlands.setEnabled(true);
            this.cbIceboundPassage.setEnabled(true);
            this.cbFreyollValley.setEnabled(true);
            this.cbKurnhold.setEnabled(true);
            this.cbUlosSprings.setEnabled(true);
            this.cbDreadWastes.setEnabled(true);
            this.cbBloodFields.setEnabled(true);
            this.cbGlaciersEdge.setEnabled(true);
            this.cbAuroraPeaks.setEnabled(true);
            this.cbGlacialGrotto.setEnabled(true);
            this.cbRoofOfTheWorld.setEnabled(true);
            this.cbDrangSulLowlands.setEnabled(true);
            this.cbDrangGalFortress.setEnabled(true);
        }
        else {
            this.cbFrostveilHighlands.setEnabled(false);
            this.cbIceboundPassage.setEnabled(false);
            this.cbFreyollValley.setEnabled(false);
            this.cbKurnhold.setEnabled(false);
            this.cbUlosSprings.setEnabled(false);
            this.cbDreadWastes.setEnabled(false);
            this.cbBloodFields.setEnabled(false);
            this.cbGlaciersEdge.setEnabled(false);
            this.cbAuroraPeaks.setEnabled(false);
            this.cbGlacialGrotto.setEnabled(false);
            this.cbRoofOfTheWorld.setEnabled(false);
            this.cbDrangSulLowlands.setEnabled(false);
            this.cbDrangGalFortress.setEnabled(false);
        }
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRiftList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharTeleportList.UID_RIFT_07_FROSTRVEIL_HIGHLANDS)) {
                this.cbFrostveilHighlands.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_07_ICEBOUND_PASSAGE)) {
                this.cbIceboundPassage.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_07_FREYOLL_VALLEY)) {
                this.cbFreyollValley.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_07_KURNHOLD)) {
                this.cbKurnhold.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_07_ULOS_SPRINGS)) {
                this.cbUlosSprings.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_07_DREAD_WASTES)) {
                this.cbDreadWastes.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_07_BLOOD_FIELDS)) {
                this.cbBloodFields.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_07_GLACIERS_EDGE)) {
                this.cbGlaciersEdge.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_07_AURORA_PEAKS)) {
                this.cbAuroraPeaks.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_07_GLACIAL_GROTTO)) {
                this.cbGlacialGrotto.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_07_ROOF_WORLDN)) {
                this.cbRoofOfTheWorld.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_07_DRANG_SUL_LOW)) {
                this.cbDrangSulLowlands.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_07_DRANG_GAL_HIGH)) {
                this.cbDrangGalFortress.setSelected(true);
            }
        }
    }
    
    private class ChangeActionListener implements ActionListener
    {
        @Override
        public void actionPerformed(final ActionEvent e) {
            GDCharRiftAct7Pane.this.changed = true;
        }
    }
}
